package com.ucenter.server.auth.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.system.comm.model.Page;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;
import com.ucenter.server.auth.dao.RoleUserDao;
import com.ucenter.server.auth.pojo.ResInfo;
import com.ucenter.server.auth.pojo.RoleInfo;
import com.ucenter.server.auth.pojo.RoleUser;
import com.ucenter.server.auth.service.RoleUserService;

/**
 * role_user的Service
 * @author autoCode
 * @date 2018-12-01 09:50:44
 * @version V1.0.0
 */
@Component
public class RoleUserServiceImpl implements RoleUserService {

	@Autowired
	private RoleUserDao roleUserDao;
	
	@Override
	public ResponseFrame save(RoleUser roleUser) {
		ResponseFrame frame = new ResponseFrame();
		RoleUser org = get(roleUser.getUserId(), roleUser.getRoleId());
		if(org == null) {
			roleUserDao.save(roleUser);
		}
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public RoleUser get(String userId, String roleId) {
		return roleUserDao.get(userId, roleId);
	}

	@Override
	public ResponseFrame pageQuery(RoleUser roleUser) {
		roleUser.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = roleUserDao.findRoleUserCount(roleUser);
		List<RoleUser> data = null;
		if(total > 0) {
			data = roleUserDao.findRoleUser(roleUser);
		}
		Page<RoleUser> page = new Page<RoleUser>(roleUser.getPage(), roleUser.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(String userId, String roleId) {
		ResponseFrame frame = new ResponseFrame();
		roleUserDao.delete(userId, roleId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public List<RoleUser> findByUserId(String userId) {
		return roleUserDao.findByUserId(userId);
	}

	@Override
	public List<ResInfo> findResByUidWid(String userId, String websiteId) {
		return roleUserDao.findResByUidWid(userId, websiteId);
	}

	@Override
	public List<RoleInfo> findRoleByUidWid(String userId, String websiteId) {
		return roleUserDao.findRoleByUidWid(userId, websiteId);
	}

	@Override
	public void deleteByUserId(String userId) {
		roleUserDao.deleteByUserId(userId);
	}
}